package com.danciyixue.boot.client.test.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.client.test.model.PreTestSaveRequest;
import com.danciyixue.boot.client.test.model.UnitTestItem;
import com.danciyixue.boot.client.test.model.UnitTestQuery;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import com.danciyixue.boot.dcyx.model.form.TestsForm;
import com.danciyixue.boot.dcyx.model.query.TestsQuery;
import com.danciyixue.boot.dcyx.model.vo.TestsVO;
import com.danciyixue.boot.dcyx.model.vo.PreTestVO;
import com.danciyixue.boot.dcyx.service.TestRecordsService;
import com.danciyixue.boot.dcyx.service.TestsService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 客户端测试控制器
 */
@Tag(name = "17. 测试", description = "测试")
@RestController
@RequestMapping("/api/client/v1/test")
@RequiredArgsConstructor
public class DcyxTestController {

    private final TestsService testsService;
    private final TestRecordsService testRecordsService;

    /**
     * 获取测试数据
     */
    @GetMapping("/page")
    public PageResult<TestsVO> page(TestsQuery queryParams) {
        IPage<TestsVO> page = testsService.getTestsPage(queryParams);
        return PageResult.success(page);
    }

    /**
     * 获取学前测试
     */
    @Operation(summary = "获取学前测试")
    @GetMapping("/pre-test/{programId}")
    public Result<PreTestVO> getPreTest(
            @Parameter(description = "教材ID", required = true)
            @PathVariable Long programId,
            @Parameter(description = "是否重新生成试卷", required = false)
            @RequestParam(value = "shuffle", defaultValue = "false") Boolean shuffle) {
        PreTestVO preTest = testsService.getPreTest(programId, shuffle);
        return Result.success(preTest);
    }


    @Operation(summary = "新增测试试卷信息")
    @PostMapping
    public Result<Void> saveTests(@RequestBody @Valid TestsForm formData ) {
        boolean result = testsService.saveTests(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取测试试卷信息表单数据")
    @GetMapping("/{id}/form")
    public Result<TestsForm> getTestsForm(
            @Parameter(description = "测试试卷信息ID") @PathVariable Long id
    ) {
        TestsForm formData = testsService.getTestsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改测试试卷信息")
    @PutMapping(value = "/{id}")
    public Result<Void> updateTests(
            @Parameter(description = "测试试卷信息ID") @PathVariable Long id,
            @RequestBody @Validated TestsForm formData
    ) {
        boolean result = testsService.updateTests(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除测试试卷信息")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteTestss(
            @Parameter(description = "测试试卷信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = testsService.deleteTestss(ids);
        return Result.judge(result);
    }

    /**
     * 获取单元测试项列表
     */
    @Operation(summary = "获取单元测试项列表")
    @GetMapping("/unit-tests")
    public PageResult<UnitTestItem> getUnitTestItems(UnitTestQuery queryParams) {
        IPage<UnitTestItem> page = testsService.getUnitTestItems(queryParams);
        return PageResult.success(page);
    }

    /**
     * 获取单元测试试卷内容
     */
    @Operation(summary = "获取单元测试试卷内容")
    @GetMapping("/unit-test/{unitId}")
    public Result<PreTestVO> getUnitTest(
            @Parameter(description = "单元ID", required = true)
            @PathVariable Long unitId,
            @Parameter(description = "是否重新生成试卷", required = false)
            @RequestParam(value = "shuffle", defaultValue = "false") Boolean shuffle) {
        PreTestVO unitTest = testsService.getUnitTest(unitId, shuffle);
        return Result.success(unitTest);
    }
}